$(document).ready(function()
    {
        $('.currency').formatCurrency();
        $.table_master = null;

        $.init_master = function()
        {
            $('#barang_master').dataTable
            ({
                "bJQueryUI": true,
                "sPaginationType": "full_numbers",
                'bServerSide'    : true,
                'bAutoWidth'     : false,
                'sAjaxSource'    : base_url + "br-02/barang/get_all_dialog",
                'aoColumns'      :
                [
                {
                    'sTitle' : 'ID Barang',
                    'mDataProp' : 'ID_BARANG'
                },
                {
                    'sTitle' : 'Nama Barang',
                    'mDataProp' : 'NAMA_BARANG'
                },
                {
                    'sTitle' : 'Merk',
                    'mDataProp' : 'MERK'
                },
                {
                    'sTitle' : 'Harga',
                    'mDataProp' : 'HARGA_BELI'
                },
                {
                    'sTitle' : 'Nama Satuan',
                    'mDataProp' : 'NAMA_SATUAN'
                },
                {
                    'sTitle' : 'Stok Minimal',
                    'mDataProp' : 'STOK_MIN'
                },
                ],
                'fnServerData': function(sSource, aoData, fnCallback)
                {
                    $.ajax
                    ({
                        'dataType': 'json',
                        'type'    : 'POST',
                        'url'     : sSource,
                        'data'    : aoData,
                        'success' : fnCallback
                    });
                }
            });
        }

        $.notif_order = function(data)
        {
            
            $("#barang_order_master tbody").html("");
            if(data.data != null)
            {
                var total = 0;
                for(var i = 0; i < data.data.length; i++)
                {
                    var newData = data.data[i];
                    total += newData.TOTAL_HARGA * 1;
                    $('.currency').formatCurrency();
                    $("#barang_order_master tbody").append('<tr>\n\
                        <td>'+newData.ID_BARANG+'</td>\n\
                        <td>'+newData.NAMA_BARANG+'</td>\n\
                        <td>'+newData.JUMLAH+'</td>\n\
                        <td class="currency">'+newData.HARGA_BELI+'</td>\n\
                        <td class="currency">'+newData.HARGA_BELI * newData.JUMLAH+'</td>\n\
                        <td><a class="button button-gray no-text" href="#" style="width:25px;" onClick="$.delete_barang(\''+ newData.ID_DETAIL_FAKTUR +'\')"> <span class="bin"></span> </a></td>\n\
                    </tr>');
                }
                $('.currency').formatCurrency();
                $("#barang_order_master tfoot").html('<tr>\n\
                        <th colspan="4">Total</th>\n\
                        <th>'+total+'</th>\n\
                        <th></th>\n\
                    </tr>');
                $("#TOTAL").val(total);
            }
        }

        $.notif_order_real = function(data)
        {
            
            $("#barang_order_master tbody").html("");
            if(data.data != null)
            {
                var total = 0;
                for(var i = 0; i < data.data.length; i++)
                {
                    var newData = data.data[i];
                    total += newData.TOTAL_HARGA * 1;
                    $("#barang_order_master tbody").append('<tr>\n\
                        <td>'+newData.ID_BARANG+'</td>\n\
                        <td>'+newData.NAMA_BARANG+'</td>\n\
                        <td>'+newData.JUMLAH+'</td>\n\
                        <td class="currency">'+newData.HARGA_BELI+'</td>\n\
                        <td class="currency">'+newData.HARGA_BELI * newData.JUMLAH+'</td>\n\
                        <td><span class="bin"></span></td>\n\
                    </tr>');
                }
                $("#barang_order_master tfoot").html('<tr>\n\
                        <th colspan="4">Total</th>\n\
                        <th class="currency">'+total+'</th>\n\
                        <th></th>\n\
                    </tr>');
                $('.currency').formatCurrency();
                $("#TOTAL").val(total);
            }
        }

        $.delete_barang = function(DETAIL_ORDER)
        {
            var yakin = confirm("Yakin hapus?");
            if (yakin){
                $.data_ajax('br-02/detail_faktur/delete_detail/' + DETAIL_ORDER, null, $.refresh_order_barang);
            }
        }

        $.refresh_order_barang = function()
        {
            $.data_ajax('br-02/detail_faktur/get_all/' + $("#ID_FAKTUR").val(), null, $.notif_order);
        }

        $.add_barang = function()
        {
            $('.currency').toNumber();
            $.data_ajax('br-06/pembelian/add_faktur_brg/' + $("#ID_FAKTUR").val(), "#barang_form", $.refresh_order_barang);
            $('#dialog-barang-form').dialog('close');
        }

        $.update_barang = function()
        {
            if($("#FILTER_MODE_TOKO").attr('checked'))
                $.call_ajax('br-02/barang/update/' + selectID + "/" + $("#ID_TOKO").val(), "#barang_form", null);
            else
                $.call_ajax('br-02/barang/update/' + selectID, "#barang_form", null);
            $('#dialog-barang-form').dialog('close');
            $.init_master();
        }

        $('#dialog-barang-form').dialog({
            title: 'Data barang',
            width: '950px',
            autoOpen:false,
            modal:true,
            resizable:false
        });

        $('#dialog-barang').dialog({
            title: 'Data barang',
            width: '950px',
            autoOpen:false,
            modal:true,
            resizable:false
        });

        $.resultbarang = function(data)
        {
            if($('td:first', data).html() != 'No matching records found')
            {
                $("#ID_BARANG").val($('td:first', data).html());
                $("#NAMA_BARANG").val($('td:eq(1)', data).html());
                $("#SATUAN").val($('td:eq(4)', data).html());
                $("#HARGA_BELI").val($('td:eq(3)', data).html());
            }
        }

        $('#barang_master tbody tr').live('click', function()
        {
            $.datatablebarangRowClicked(this);
        });

        $.datatablebarangRowClicked = function(that)
        {
            $.resultbarang(that);
            $('#dialog-barang').dialog('close');
        }

        $.add_barang_form = function()
        {
            $.clear_form("barang_form");
            $("#NO_FAKTUR").val($("#order_no").val());
            $("#add_submit").show();
            $("#update_submit").hide();
            $('#dialog-barang-form').dialog('open');
        }

        $("#barang_form").submit(function() {
            return false;
        });

        $("#order-faktur").submit(function() {
            return false;
        });

        $('#ID_BARANG').click(function(){
            $('#dialog-barang').dialog('open');
        });

        $("#barang_add").click($.add_barang_form);
        $("#ID_BARANG").click($.add_barang_form);
        $("#add_barang").click($.add_barang);

        $.notif_add_order = function(data)
        {
            if(data.status == "OK")
            {
                $("#ID_FAKTUR").val(data.data);
            }
            else
            {
                if (data.status == "REAL"){
                    if (confirm("Nomer Faktur sudah terdaftar, lanjutkan?")){
                        var current_order = data.data[0];
                        $("#ID_FAKTUR").val(current_order.ID_FAKTUR);
                        $("#NOMOR_FAKTUR").val(current_order.NOMOR_FAKTUR);
                        $("#SUPLIER").val(current_order.ID_SUPLIER);
                        $("#TGL_FAKTUR").val(current_order.TGL_FAKTUR);
                        $("#DISKON").val(current_order.DISKON);
                        $("#PPN").val(current_order.PPN);
                        $("#ALL_TOTAL").val(current_order.TOTAL);
                        $("#barang_add").attr("disabled", "disabled");
                        $("#update_faktur").attr("disabled", "disabled");
                        $.data_ajax('br-02/detail_faktur/get_all/' + $("#ID_FAKTUR").val(), null, $.notif_order_real);
                    }else window.location = base_url + "br-06/pembelian/faktur";
                }else {
                        var current_order = data.data[0];
                        $("#ID_FAKTUR").val(current_order.ID_FAKTUR);
                        $("#NOMOR_FAKTUR").val(current_order.NOMOR_FAKTUR);
                        $("#SUPLIER").val(current_order.ID_SUPLIER);
                        $("#TGL_FAKTUR").val(current_order.TGL_FAKTUR);
                        $("#DISKON").val(current_order.DISKON);
                        $("#PPN").val(current_order.PPN);
                        $("#ALL_TOTAL").val(current_order.TOTAL);
                        $.data_ajax('br-02/detail_faktur/get_all/' + $("#ID_FAKTUR").val(), null, $.notif_order);
                }
            }
        }


        $("#add_faktur").click(function(){
            $("#add_faktur").hide();
            $("#barang_add").show();
            $("#barang_order_master").show();
            $.data_ajax('br-06/pembelian/add_faktur', '#order-faktur', $.notif_add_order);
        }
        );

        $.notif_update = function()
        {
            alert("Data telah tersimpan");
            window.location = base_url + "br-06/pembelian/faktur/";
        }

        $("#update_faktur").click(function(){
            $.call_ajax('br-06/add_stok_barang/' + $("#ID_FAKTUR").val(), '#order-faktur', null);
            $.data_ajax('br-02/faktur/update/' + $("#ID_FAKTUR").val(), '#order-faktur', $.notif_update);
        }
        );

        $.generate_no_order = function()
        {
            
        }

        $.calculate_total = function()
        {
            $('.currency').toNumber();
            var unit = $("#UNIT").val();
            var harga = $("#HARGA_BELI").val();
            $("#HARGA").val(unit * harga);
        }

        $.calculate_all = function()
        {
            $('.currency').toNumber();
            var total = $("#TOTAL").val();
            var diskon = $("#DISKON").val();
            var ppn = $("#PPN").val();

            diskon = (diskon <= 100) ? diskon * total : diskon;
            ppn = (ppn <= 100) ? ppn * total : ppn;

            $("#ALL_TOTAL").val(total - diskon + ppn *1);
        }

        $.init_master();
        $.generate_no_order();

        $("#TGL_ORDER").click($.generate_no_order);
        $("#ID_TOKO").click($.generate_no_order);

        $("#UNIT").blur($.calculate_total);

        $("#DISKON").blur($.calculate_all);
        $("#PPN").blur($.calculate_all);

        $("#barang_add").hide();
        $("#barang_order_master").hide();
    }
    );